package com.linewell.license.demo.controller;

import com.alibaba.fastjson.JSONArray;
import com.linewell.license.demo.constants.AipVersionConstant;
import com.linewell.license.demo.utils.CurrentRequestUtils;
import com.linewell.license.demo.vo.MenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * Company:
 * Createtime : 2019/8/15 14:00
 * Description :
 */
@Api(tags = {"获取菜单接口"})
@RestController
@RequestMapping(AipVersionConstant.API + AipVersionConstant.API_VERSION_01 + "/menu")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    @ApiOperation("获取菜单列表")
    @RequestMapping(method = RequestMethod.GET)
    public List<MenuVo> get() throws Exception{
        String role = CurrentRequestUtils.getLoginInfo().getRole();
        return this.getDefaultMenus(role);
    }

    /**
     * 根据角色获取默认的资源
     * @param role 角色
     */
    private List<MenuVo> getDefaultMenus(String role){
        List<MenuVo> result = new ArrayList<>();
        InputStream in = null;
        try {
            ResourceLoader resourceLoader = new DefaultResourceLoader();
            Resource resource = resourceLoader.getResource("classpath:config/menu.json");
            in = resource.getInputStream();
            String content = IOUtils.toString(in, Charset.defaultCharset());
            List<MenuVo> menus = JSONArray.parseArray(content , MenuVo.class);
            for(MenuVo menuVo : menus){
                MenuVo menu = this.permissionMenu(role , menuVo);
                if(menu == null){
                    continue;
                }
                //一级菜单
                if(menuVo.getChildren() == null || menuVo.getChildren().isEmpty()){
                    result.add(menu);
                    continue;
                }
                List<MenuVo> children = new ArrayList<>();
                for(MenuVo vo : menuVo.getChildren()){
                    MenuVo child = this.permissionMenu(role , vo);
                    if(child != null){
                        children.add(child);
                    }
                }
                menu.setChildren(children);
                result.add(menu);
            }
        }catch(FileNotFoundException e) {
            logger.debug(e.getMessage() , e);
        }catch(Exception e) {
            logger.error(e.getMessage() , e);
        }finally {
            IOUtils.closeQuietly(in);
        }
        return result;
    }

    private MenuVo permissionMenu(String role ,MenuVo menuVo){
        //一级菜单如果没有权限则后续的都没有权限
        if(menuVo.getRole() != null && !menuVo.getRole().contains(role)){
            return null;
        }
        MenuVo menu = new MenuVo();
        menu.setName(menuVo.getName());
        menu.setImage(menuVo.getImage());
        menu.setUrl(menuVo.getUrl());
        menu.setPermission(menu.getPermission());
        return menu;
    }

}
